﻿namespace SmartSoft.Controls.ChildGridControls
{
    using SmartSoft;
    using SmartSoft.Controls;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class SearchCell : DataGridViewTextBoxCell
    {
        private bool ib_CheckMovable = true;
        private bool ib_EmptyIsValid;
        private bool ib_FoundIsValid = true;
        private AppInstance.TextControlLength ie_UserTextLength = AppInstance.TextControlLength.ItemCode;
        private AppInstance.TextControlTrimMode ie_UserTrimMode = AppInstance.TextControlTrimMode.Right;
        private string is_DestinationFields = "";
        private string is_FirstFilter = "";
        private string is_SourceFields = "";
        private string is_TableName = "";
        private Color oDisabledForeColor = Color.Blue;

        public override object Clone()
        {
            SearchCell cell = base.Clone() as SearchCell;
            if (cell != null)
            {
                cell.DisabledForeColor = this.DisabledForeColor;
                cell.EmptyIsValid = this.EmptyIsValid;
                cell.FoundIsValid = this.FoundIsValid;
                cell.TableName = this.TableName;
                cell.FirstFilter = this.FirstFilter;
                cell.SourceFields = this.SourceFields;
                cell.DestinationFields = this.DestinationFields;
                cell.UserTrimMode = this.UserTrimMode;
                cell.UserTextLength = this.UserTextLength;
                cell.CheckMovable = this.CheckMovable;
            }
            return cell;
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            TextSearch editingControl = base.DataGridView.EditingControl as TextSearch;
            if (editingControl != null)
            {
                editingControl.BorderStyle = BorderStyle.None;
                editingControl.DisabledForeColor = this.DisabledForeColor;
                editingControl.EmptyIsValid = this.EmptyIsValid;
                editingControl.FoundIsValid = this.FoundIsValid;
                editingControl.TableName = this.TableName;
                editingControl.FirstFilter = this.FirstFilter;
                editingControl.SourceFields = this.SourceFields;
                editingControl.DestinationFields = this.DestinationFields;
                editingControl.UserTrimMode = this.UserTrimMode;
                editingControl.UserTextLength = this.UserTextLength;
                editingControl.CheckMovable = this.CheckMovable;
                string str = initialFormattedValue as string;
                if (str == null)
                {
                    editingControl.Text = string.Empty;
                }
                else
                {
                    editingControl.Text = str;
                }
            }
        }

        private void OnCommonChange()
        {
            if (((base.DataGridView != null) && !base.DataGridView.IsDisposed) && !base.DataGridView.Disposing)
            {
                if (base.RowIndex == -1)
                {
                    base.DataGridView.InvalidateColumn(base.ColumnIndex);
                }
                else
                {
                    base.DataGridView.UpdateCellValue(base.ColumnIndex, base.RowIndex);
                }
            }
        }

        private bool OwnsEditingSearchControl(int rowIndex)
        {
            if ((rowIndex == -1) || (base.DataGridView == null))
            {
                return false;
            }
            SearchEditing editingControl = base.DataGridView.EditingControl as SearchEditing;
            return ((editingControl != null) && (rowIndex == editingControl.EditingControlRowIndex));
        }

        internal void SetCheckMovable(int rowIndex, bool value)
        {
            this.ib_CheckMovable = value;
            if (this.OwnsEditingSearchControl(rowIndex))
            {
                this.EditingSearchControl.CheckMovable = value;
            }
        }

        internal void SetEmptyIsValid(int rowIndex, bool value)
        {
            this.ib_EmptyIsValid = value;
            if (this.OwnsEditingSearchControl(rowIndex))
            {
                this.EditingSearchControl.EmptyIsValid = value;
            }
        }

        internal void SetFirstFilter(int rowIndex, string value)
        {
            this.is_FirstFilter = value;
            if (this.OwnsEditingSearchControl(rowIndex))
            {
                this.EditingSearchControl.FirstFilter = value;
            }
        }

        internal void SetFoundIsValid(int rowIndex, bool value)
        {
            this.ib_FoundIsValid = value;
            if (this.OwnsEditingSearchControl(rowIndex))
            {
                this.EditingSearchControl.FoundIsValid = value;
            }
        }

        internal void SetTableName(int rowIndex, string value)
        {
            this.is_TableName = value;
            if (this.OwnsEditingSearchControl(rowIndex))
            {
                this.EditingSearchControl.TableName = value;
            }
        }

        internal void SetUserTextLength(int rowIndex, AppInstance.TextControlLength value)
        {
            this.ie_UserTextLength = value;
            if (this.OwnsEditingSearchControl(rowIndex))
            {
                this.EditingSearchControl.UserTextLength = value;
            }
        }

        internal void SetUserTrimMode(int rowIndex, AppInstance.TextControlTrimMode value)
        {
            this.ie_UserTrimMode = value;
            if (this.OwnsEditingSearchControl(rowIndex))
            {
                this.EditingSearchControl.UserTrimMode = value;
            }
        }

        public bool CheckMovable
        {
            get
            {
                return this.ib_CheckMovable;
            }
            set
            {
                if (this.ib_CheckMovable != value)
                {
                    this.SetCheckMovable(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public string DestinationFields
        {
            get
            {
                return this.is_DestinationFields;
            }
            set
            {
                if (this.is_DestinationFields != value)
                {
                    this.is_DestinationFields = value;
                    if (this.OwnsEditingSearchControl(base.RowIndex))
                    {
                        this.EditingSearchControl.DestinationFields = value;
                    }
                }
            }
        }

        public Color DisabledForeColor
        {
            get
            {
                return this.oDisabledForeColor;
            }
            set
            {
                if (this.oDisabledForeColor != value)
                {
                    this.oDisabledForeColor = value;
                    if (this.OwnsEditingSearchControl(base.RowIndex))
                    {
                        this.EditingSearchControl.DisabledForeColor = value;
                    }
                }
            }
        }

        private SearchEditing EditingSearchControl
        {
            get
            {
                return (base.DataGridView.EditingControl as SearchEditing);
            }
        }

        public override System.Type EditType
        {
            get
            {
                return typeof(SearchEditing);
            }
        }

        public bool EmptyIsValid
        {
            get
            {
                return this.ib_EmptyIsValid;
            }
            set
            {
                if (this.ib_EmptyIsValid != value)
                {
                    this.SetEmptyIsValid(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public string FirstFilter
        {
            get
            {
                return this.is_FirstFilter;
            }
            set
            {
                if (this.is_FirstFilter != value)
                {
                    this.SetFirstFilter(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public bool FoundIsValid
        {
            get
            {
                return this.ib_FoundIsValid;
            }
            set
            {
                if (this.ib_FoundIsValid != value)
                {
                    this.SetFoundIsValid(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public string SourceFields
        {
            get
            {
                return this.is_SourceFields;
            }
            set
            {
                if (this.is_SourceFields != value)
                {
                    this.is_SourceFields = value;
                    if (this.OwnsEditingSearchControl(base.RowIndex))
                    {
                        this.EditingSearchControl.SourceFields = value;
                    }
                }
            }
        }

        public string TableName
        {
            get
            {
                return this.is_TableName;
            }
            set
            {
                if (this.is_TableName != value)
                {
                    this.SetTableName(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public AppInstance.TextControlLength UserTextLength
        {
            get
            {
                return this.ie_UserTextLength;
            }
            set
            {
                if (this.ie_UserTextLength != value)
                {
                    this.SetUserTextLength(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public AppInstance.TextControlTrimMode UserTrimMode
        {
            get
            {
                return this.ie_UserTrimMode;
            }
            set
            {
                if (this.ie_UserTrimMode != value)
                {
                    this.SetUserTrimMode(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public override System.Type ValueType
        {
            get
            {
                return typeof(string);
            }
        }
    }
}

